﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Project.dalSql;
using WISD.Common;
using WISD.CarFriend.Model;
using WISD.DBUtility.DBOperate;

namespace WISD.CarFriend.BLL.OA
{
    public class ReturnVisitModule
    {
        #region MaticCode

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ReturnVisitID", "T_CF_OA_ReturnVisit");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ReturnVisitID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_CF_OA_ReturnVisit");
            strSql.Append(" where ReturnVisitID=@ReturnVisitID");
            SqlParameter[] parameters = {
					new SqlParameter("@ReturnVisitID", SqlDbType.Int,4)
			};
            parameters[0].Value = ReturnVisitID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(WISD.CarFriend.Model.CFModel.T_CF_OA_ReturnVisit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CF_OA_ReturnVisit(");
            strSql.Append("ConsumeID,IsComplaint,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8)");
            strSql.Append(" values (");
            strSql.Append("@ConsumeID,@IsComplaint,@ReturnVisitType,@StartTime,@EndTime,@Complaint,@Result,@Satisfaction,@Memo,@State,@SendEmployee,@Reviser,@ReviseDate,@Memo1,@Memo2,@Memo3,@Memo4,@Memo5,@Memo6,@Memo7,@Memo8)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ConsumeID", SqlDbType.Int,4),
					new SqlParameter("@IsComplaint", SqlDbType.Int,4),
					new SqlParameter("@ReturnVisitType", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Complaint", SqlDbType.NVarChar,1000),
					new SqlParameter("@Result", SqlDbType.NChar,1000),
					new SqlParameter("@Satisfaction", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@SendEmployee", SqlDbType.NVarChar,100),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.ConsumeID;
            parameters[1].Value = model.IsComplaint;
            parameters[2].Value = model.ReturnVisitType;
            parameters[3].Value = model.StartTime;
            parameters[4].Value = model.EndTime;
            parameters[5].Value = model.Complaint;
            parameters[6].Value = model.Result;
            parameters[7].Value = model.Satisfaction;
            parameters[8].Value = model.Memo;
            parameters[9].Value = model.State;
            parameters[10].Value = model.SendEmployee;
            parameters[11].Value = model.Reviser;
            parameters[12].Value = model.ReviseDate;
            parameters[13].Value = model.Memo1;
            parameters[14].Value = model.Memo2;
            parameters[15].Value = model.Memo3;
            parameters[16].Value = model.Memo4;
            parameters[17].Value = model.Memo5;
            parameters[18].Value = model.Memo6;
            parameters[19].Value = model.Memo7;
            parameters[20].Value = model.Memo8;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(WISD.CarFriend.Model.CFModel.T_CF_OA_ReturnVisit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CF_OA_ReturnVisit set ");
            strSql.Append("ConsumeID=@ConsumeID,");
            strSql.Append("IsComplaint=@IsComplaint,");
            strSql.Append("ReturnVisitType=@ReturnVisitType,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("Complaint=@Complaint,");
            strSql.Append("Result=@Result,");
            strSql.Append("Satisfaction=@Satisfaction,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("State=@State,");
            strSql.Append("SendEmployee=@SendEmployee,");
            strSql.Append("Reviser=@Reviser,");
            strSql.Append("ReviseDate=@ReviseDate,");
            strSql.Append("Memo1=@Memo1,");
            strSql.Append("Memo2=@Memo2,");
            strSql.Append("Memo3=@Memo3,");
            strSql.Append("Memo4=@Memo4,");
            strSql.Append("Memo5=@Memo5,");
            strSql.Append("Memo6=@Memo6,");
            strSql.Append("Memo7=@Memo7,");
            strSql.Append("Memo8=@Memo8");
            strSql.Append(" where ReturnVisitID=@ReturnVisitID");
            SqlParameter[] parameters = {
					new SqlParameter("@ConsumeID", SqlDbType.Int,4),
					new SqlParameter("@IsComplaint", SqlDbType.Int,4),
					new SqlParameter("@ReturnVisitType", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Complaint", SqlDbType.NVarChar,1000),
					new SqlParameter("@Result", SqlDbType.NChar,1000),
					new SqlParameter("@Satisfaction", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@SendEmployee", SqlDbType.NVarChar,100),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100),
					new SqlParameter("@ReturnVisitID", SqlDbType.Int,4)};
            parameters[0].Value = model.ConsumeID;
            parameters[1].Value = model.IsComplaint;
            parameters[2].Value = model.ReturnVisitType;
            parameters[3].Value = model.StartTime;
            parameters[4].Value = model.EndTime;
            parameters[5].Value = model.Complaint;
            parameters[6].Value = model.Result;
            parameters[7].Value = model.Satisfaction;
            parameters[8].Value = model.Memo;
            parameters[9].Value = model.State;
            parameters[10].Value = model.SendEmployee;
            parameters[11].Value = model.Reviser;
            parameters[12].Value = model.ReviseDate;
            parameters[13].Value = model.Memo1;
            parameters[14].Value = model.Memo2;
            parameters[15].Value = model.Memo3;
            parameters[16].Value = model.Memo4;
            parameters[17].Value = model.Memo5;
            parameters[18].Value = model.Memo6;
            parameters[19].Value = model.Memo7;
            parameters[20].Value = model.Memo8;
            parameters[21].Value = model.ReturnVisitID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ReturnVisitID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_ReturnVisit ");
            strSql.Append(" where ReturnVisitID=@ReturnVisitID");
            SqlParameter[] parameters = {
					new SqlParameter("@ReturnVisitID", SqlDbType.Int,4)
			};
            parameters[0].Value = ReturnVisitID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ReturnVisitIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_ReturnVisit ");
            strSql.Append(" where ReturnVisitID in (" + ReturnVisitIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WISD.CarFriend.Model.CFModel.T_CF_OA_ReturnVisit GetModel(int ReturnVisitID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ReturnVisitID,ConsumeID,IsComplaint,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 from T_CF_OA_ReturnVisit ");
            strSql.Append(" where ReturnVisitID=@ReturnVisitID");
            SqlParameter[] parameters = {
					new SqlParameter("@ReturnVisitID", SqlDbType.Int,4)
			};
            parameters[0].Value = ReturnVisitID;

            WISD.CarFriend.Model.CFModel.T_CF_OA_ReturnVisit model = new WISD.CarFriend.Model.CFModel.T_CF_OA_ReturnVisit();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ReturnVisitID"] != null && ds.Tables[0].Rows[0]["ReturnVisitID"].ToString() != "")
                {
                    model.ReturnVisitID = int.Parse(ds.Tables[0].Rows[0]["ReturnVisitID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ConsumeID"] != null && ds.Tables[0].Rows[0]["ConsumeID"].ToString() != "")
                {
                    model.ConsumeID = int.Parse(ds.Tables[0].Rows[0]["ConsumeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsComplaint"] != null && ds.Tables[0].Rows[0]["IsComplaint"].ToString() != "")
                {
                    model.IsComplaint = int.Parse(ds.Tables[0].Rows[0]["IsComplaint"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ReturnVisitType"] != null && ds.Tables[0].Rows[0]["ReturnVisitType"].ToString() != "")
                {
                    model.ReturnVisitType = int.Parse(ds.Tables[0].Rows[0]["ReturnVisitType"].ToString());
                }
                if (ds.Tables[0].Rows[0]["StartTime"] != null && ds.Tables[0].Rows[0]["StartTime"].ToString() != "")
                {
                    model.StartTime = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EndTime"] != null && ds.Tables[0].Rows[0]["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Complaint"] != null && ds.Tables[0].Rows[0]["Complaint"].ToString() != "")
                {
                    model.Complaint = ds.Tables[0].Rows[0]["Complaint"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Result"] != null && ds.Tables[0].Rows[0]["Result"].ToString() != "")
                {
                    model.Result = ds.Tables[0].Rows[0]["Result"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Satisfaction"] != null && ds.Tables[0].Rows[0]["Satisfaction"].ToString() != "")
                {
                    model.Satisfaction = int.Parse(ds.Tables[0].Rows[0]["Satisfaction"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo"] != null && ds.Tables[0].Rows[0]["Memo"].ToString() != "")
                {
                    model.Memo = ds.Tables[0].Rows[0]["Memo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["State"] != null && ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SendEmployee"] != null && ds.Tables[0].Rows[0]["SendEmployee"].ToString() != "")
                {
                    model.SendEmployee = ds.Tables[0].Rows[0]["SendEmployee"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Reviser"] != null && ds.Tables[0].Rows[0]["Reviser"].ToString() != "")
                {
                    model.Reviser = ds.Tables[0].Rows[0]["Reviser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ReviseDate"] != null && ds.Tables[0].Rows[0]["ReviseDate"].ToString() != "")
                {
                    model.ReviseDate = DateTime.Parse(ds.Tables[0].Rows[0]["ReviseDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo1"] != null && ds.Tables[0].Rows[0]["Memo1"].ToString() != "")
                {
                    model.Memo1 = ds.Tables[0].Rows[0]["Memo1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo2"] != null && ds.Tables[0].Rows[0]["Memo2"].ToString() != "")
                {
                    model.Memo2 = ds.Tables[0].Rows[0]["Memo2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo3"] != null && ds.Tables[0].Rows[0]["Memo3"].ToString() != "")
                {
                    model.Memo3 = ds.Tables[0].Rows[0]["Memo3"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo4"] != null && ds.Tables[0].Rows[0]["Memo4"].ToString() != "")
                {
                    model.Memo4 = ds.Tables[0].Rows[0]["Memo4"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo5"] != null && ds.Tables[0].Rows[0]["Memo5"].ToString() != "")
                {
                    model.Memo5 = ds.Tables[0].Rows[0]["Memo5"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo6"] != null && ds.Tables[0].Rows[0]["Memo6"].ToString() != "")
                {
                    model.Memo6 = ds.Tables[0].Rows[0]["Memo6"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo7"] != null && ds.Tables[0].Rows[0]["Memo7"].ToString() != "")
                {
                    model.Memo7 = ds.Tables[0].Rows[0]["Memo7"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo8"] != null && ds.Tables[0].Rows[0]["Memo8"].ToString() != "")
                {
                    model.Memo8 = ds.Tables[0].Rows[0]["Memo8"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ReturnVisitID,ConsumeID,IsComplaint,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_ReturnVisit ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ReturnVisitID,ConsumeID,IsComplaint,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_ReturnVisit ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_CF_OA_ReturnVisit ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ReturnVisitID desc");
            }
            strSql.Append(")AS Row, T.*  from T_CF_OA_ReturnVisit T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion


        #region SelfCode
        public static SqlObject sqlobj = null;
        public static DataTable dt = null;
        private static List<SqlObject> SqlObj = null;

        /// <summary>
        /// 选择会员信息
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static string GetMemberInfo(int PageSize, int PageIndex, string condition)
        {
            StringBuilder tblName = new StringBuilder(); //从那些表里面进行查询
            int RsCount = 0;
            tblName.Append(" T_CF_OA_Member a ");

            string strOrder = " a.MemberID desc ";  //排序的字段
            StringBuilder strGetFields = new StringBuilder(); //返回的列datediff(d,updatedate,getdate())
            strGetFields.Append(" a.MemberID+'#'+a.Name  main, a.MemberID,a.Name   ");

            StringBuilder strWhere = new StringBuilder(); //查询的条件
            strWhere.Append(" 1=1 ");

            //如果查询条件不为空的话
            if (!condition.Equals(""))
            {
                /*var Arr = condition.Split('@'); //分割成数组
                var type = Arr[0].ToString();
                var enterprise = Arr[1].ToString();
                if (!type.Equals("0"))
                {
                    strWhere.AppendFormat("  and a.ClientPro like N\'%{0}%\'  ", type);
                }
                if (!enterprise.Equals("0"))
                {

                    strWhere.AppendFormat("  and a.C_name like N\'%{0}%\' ", enterprise);
                }*/
            }
            object[] obj = new object[] { tblName.ToString(), strGetFields.ToString(), strOrder, PageSize, PageIndex, strWhere };
            DataTable dt = new DataTable();
            dt = dalSqlHelper.ExecuteProcedure(obj, ref RsCount);
            if (dt.Rows.Count > 0)
            {
                return dalSqlHelper.DtToSON(dt) + "@" + RsCount.ToString();
            }
            else
            {
                return "";
            }

        }


        /// <summary>
        ///根据会员编号得到会员名称
        /// </summary>
        /// <returns></returns>
        public static string GetCardInfo(string MemberID)
        {
            sqlobj = new SqlObject();
            StringBuilder sb = new StringBuilder();
            sb.Append("select Name from T_CF_OA_Member where MemberID=@MemberID ");

            sqlobj.Sql = sb.ToString();
            sqlobj.PramS = new SqlParameter[] { 
               new SqlParameter("@MemberID",MemberID)
            };
            dt = dalSqlHelper.ExecuteSelect(sqlobj);
            return dalSqlHelper.DtToSON(dt);
        }

        /// <summary>
        /// 获取修改历史记录
        /// </summary>
        /// <returns></returns>
        public static string GetUpdateLog(string ReturnVisitID)
        {
            sqlobj = new SqlObject();
            StringBuilder sb = new StringBuilder();
            sb.Append(" select Reviser,ReviseDate from T_CF_OA_ReturnVisit_Log where ReturnVisitID = @ReturnVisitID ");

            sqlobj.Sql = sb.ToString();
            sqlobj.PramS = new SqlParameter[] { 
               new SqlParameter("@ReturnVisitID",ReturnVisitID)
            };
            dt = dalSqlHelper.ExecuteSelect(sqlobj);
            return dalSqlHelper.DtToSON(dt);
        }


        /// <summary>
        ///  初始化页面和查询数据
        /// </summary>
        /// <param name="PageSize">每页显示的条数</param>
        /// <param name="PageIndex">当前页码</param>
        /// <param name="condition">搜索条件</param>
        /// <returns></returns>
        public static string GetData(int PageSize, int PageIndex, string condition)
        {
            StringBuilder tblName = new StringBuilder(); //从那些表里面进行查询
            int RsCount = 0;
            tblName.Append(" T_CF_OA_ReturnVisit AS a LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_Consume AS b ON a.ConsumeID = b.ConsumeID LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_MemberConsumeItem AS c ON b.ConsumeIDItem = c.Id LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_MemberCard AS d ON b.MemberCardID = d.MemberCardID LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_Member AS e ON d.MemberID = e.MemberID ");

            string strOrder = " a.ReturnVisitID desc ";  //排序的字段
            StringBuilder strGetFields = new StringBuilder(); //返回的列datediff(d,updatedate,getdate())
            strGetFields.Append(" a.ReturnVisitID main,b.MemberCardID,e.Name,c.ConsumeItem, ");
            strGetFields.Append(" case when a.ReturnVisitType ='1' then '电话' end as ReturnVisitType, ");
            strGetFields.Append(" a.StartTime, a.EndTime, ");
            strGetFields.Append(" case when a.Satisfaction = '1' then '不满意' else '满意' end as Satisfaction, ");
            strGetFields.Append(" case when a.State ='1' then '登记状态' when a.State ='2' then '执行状态' else '完成状态' end as State ");

            StringBuilder strWhere = new StringBuilder(); //查询的条件
            strWhere.Append(" 1=1 ");

            //如果查询条件不为空的话
            if (!condition.Equals(""))
            {
                var Arr = condition.Split('@');
                var Sel_Satisfaction = Arr[0];  //会员编号
                var Sel_State = Arr[1];  //状态
                var IsComplaint = Arr[2];  //状态

                if (!Sel_Satisfaction.Equals("-1"))
                {
                    strWhere.AppendFormat("  and a.Satisfaction =  N\'{0}\'", Sel_Satisfaction);
                }
                if (!Sel_State.Equals("0"))
                {
                    strWhere.AppendFormat("  and a.State like N\'%{0}%\'", Sel_State);
                }
                if (!IsComplaint.Equals("0"))
                {
                    strWhere.AppendFormat("  and a.IsComplaint = N\'{0}\'", IsComplaint);
                }
            }
            object[] obj = new object[] { tblName.ToString(), strGetFields.ToString(), strOrder, PageSize, PageIndex, strWhere.ToString() };
            DataTable dt = new DataTable();

            dt = dalSqlHelper.ExecuteProcedure(obj, ref RsCount);
            if (dt.Rows.Count > 0)
            {
                return dalSqlHelper.DtToSON(dt) + "@" + RsCount.ToString();
            }
            else
            {
                return "@0";
            }
        }


        /// <summary>
        /// 添加客户实体
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string AddEntity(WISD.CarFriend.Model.CFModel.T_CF_OA_ReturnVisit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CF_OA_ReturnVisit(");
            strSql.Append("ConsumeID,IsComplaint,ReturnVisitType,StartTime,EndTime,Complaint,Result,Satisfaction,Memo,State,SendEmployee,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8)");
            strSql.Append(" values (");
            strSql.Append("@ConsumeID,@IsComplaint,@ReturnVisitType,@StartTime,@EndTime,@Complaint,@Result,@Satisfaction,@Memo,@State,@SendEmployee,@Reviser,@ReviseDate,@Memo1,@Memo2,@Memo3,@Memo4,@Memo5,@Memo6,@Memo7,@Memo8)");

            SqlParameter[] parameters = {
					new SqlParameter("@ConsumeID", SqlDbType.NVarChar,50),
                    new SqlParameter("@IsComplaint", SqlDbType.Int,4),
					new SqlParameter("@ReturnVisitType", SqlDbType.Int,4),
					new SqlParameter("@StartTime", SqlDbType.DateTime),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Complaint", SqlDbType.NVarChar,1000),
					new SqlParameter("@Result", SqlDbType.NChar,1000),
					new SqlParameter("@Satisfaction", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@SendEmployee", SqlDbType.NVarChar,100),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.ConsumeID;
            parameters[1].Value = model.IsComplaint;
            parameters[2].Value = model.ReturnVisitType;
            parameters[3].Value = model.StartTime;
            parameters[4].Value = model.EndTime;
            parameters[5].Value = model.Complaint;
            parameters[6].Value = model.Result;
            parameters[7].Value = model.Satisfaction;
            parameters[8].Value = model.Memo;
            parameters[9].Value = model.State;
            parameters[10].Value = model.SendEmployee;
            parameters[11].Value = model.Reviser;
            parameters[12].Value = model.ReviseDate;
            parameters[13].Value = model.Memo1;
            parameters[14].Value = model.Memo2;
            parameters[15].Value = model.Memo3;
            parameters[16].Value = model.Memo4;
            parameters[17].Value = model.Memo5;
            parameters[18].Value = model.Memo6;
            parameters[19].Value = model.Memo7;
            parameters[20].Value = model.Memo8;

            SqlObject sqlObject = new SqlObject();
            sqlObject.Sql = strSql.ToString();
            sqlObject.PramS = parameters;
            List<SqlObject> sqlObjectList = new List<SqlObject>();
            sqlObjectList.Add(sqlObject);

            if (dalSqlHelper.ExecuteTransaction(sqlObjectList))
            {
                return "Success";
            }
            else
            {
                return "Failure";
            }
        }

        /// <summary>
        /// 获取投诉编号
        /// </summary>
        /// <returns></returns>
        public static string GetMemberId()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss");
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ClientID"></param>
        /// <returns></returns>
        public static string DeleteEntity(string ReturnVisitID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_ReturnVisit ");
            strSql.Append(" where ReturnVisitID=@ReturnVisitID");
            SqlParameter[] parameters = {
					new SqlParameter("@ReturnVisitID", SqlDbType.Int,4)
			};
            parameters[0].Value = ReturnVisitID;

            SqlObject sqlObject = new SqlObject();
            sqlObject.Sql = strSql.ToString();
            sqlObject.PramS = parameters;
            List<SqlObject> sqlObjectList = new List<SqlObject>();
            sqlObjectList.Add(sqlObject);

            if (dalSqlHelper.ExecuteTransaction(sqlObjectList))
            {
                return "Success";
            }
            else
            {
                return "Failure";
            }
        }


        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string EditEntity(WISD.CarFriend.Model.CFModel.T_CF_OA_ReturnVisit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CF_OA_ReturnVisit set ");
            strSql.Append("ReturnVisitType=@ReturnVisitType,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("Complaint=@Complaint,");
            strSql.Append("Result=@Result,");
            strSql.Append("Satisfaction=@Satisfaction,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("State=@State,");
            strSql.Append("SendEmployee=@SendEmployee,");
            strSql.Append("Reviser=@Reviser,");
            strSql.Append("ReviseDate=@ReviseDate,");
            strSql.Append("Memo1=@Memo1,");
            strSql.Append("Memo2=@Memo2,");
            strSql.Append("Memo3=@Memo3,");
            strSql.Append("Memo4=@Memo4,");
            strSql.Append("Memo5=@Memo5,");
            strSql.Append("Memo6=@Memo6,");
            strSql.Append("Memo7=@Memo7,");
            strSql.Append("Memo8=@Memo8,");
            strSql.Append("IsComplaint=@IsComplaint");
            strSql.Append(" where ReturnVisitID=@ReturnVisitID");
            SqlParameter[] parameters = {
					new SqlParameter("@ReturnVisitType", SqlDbType.Int,4),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@Complaint", SqlDbType.NVarChar,1000),
					new SqlParameter("@Result", SqlDbType.NChar,1000),
					new SqlParameter("@Satisfaction", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@SendEmployee", SqlDbType.NVarChar,100),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100),
                    new SqlParameter("@IsComplaint", SqlDbType.Int,4),
					new SqlParameter("@ReturnVisitID", SqlDbType.Int,4)};
            parameters[0].Value = model.ReturnVisitType;
            parameters[1].Value = model.EndTime;
            parameters[2].Value = model.Complaint;
            parameters[3].Value = model.Result;
            parameters[4].Value = model.Satisfaction;
            parameters[5].Value = model.Memo;
            parameters[6].Value = model.State;
            parameters[7].Value = model.SendEmployee;
            parameters[8].Value = model.Reviser;
            parameters[9].Value = model.ReviseDate;
            parameters[10].Value = model.Memo1;
            parameters[11].Value = model.Memo2;
            parameters[12].Value = model.Memo3;
            parameters[13].Value = model.Memo4;
            parameters[14].Value = model.Memo5;
            parameters[15].Value = model.Memo6;
            parameters[16].Value = model.Memo7;
            parameters[17].Value = model.Memo8;
            parameters[18].Value = model.IsComplaint;
            parameters[19].Value = model.ReturnVisitID;

            SqlObject sqlObject = new SqlObject();
            sqlObject.Sql = strSql.ToString();
            sqlObject.PramS = parameters;
            List<SqlObject> sqlObjectList = new List<SqlObject>();
            sqlObjectList.Add(sqlObject);

            if (dalSqlHelper.ExecuteTransaction(sqlObjectList))
            {
                return "Success";
            }
            else
            {
                return "Failure";
            }
        }


        /// <summary>
        /// 修改时查询该条记录
        /// </summary>
        /// <param name="ClientID"></param>
        /// <returns></returns>
        public static string GetOne(string PK)
        {
            StringBuilder tblName = new StringBuilder(); //从那些表里面进行查询
            int RsCount = 0;
            tblName.Append(" T_CF_OA_ReturnVisit AS a LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_Consume AS b ON a.ConsumeID = b.ConsumeID LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_MemberConsumeItem AS c ON b.ConsumeIDItem = c.Id LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_MemberCard AS d ON b.MemberCardID = d.MemberCardID LEFT OUTER JOIN ");
            tblName.Append(" T_CF_OA_Member AS e ON d.MemberID = e.MemberID ");


            string strOrder = " a.ReturnVisitID desc ";  //排序的字段
            StringBuilder strGetFields = new StringBuilder(); //返回的列datediff(d,updatedate,getdate())

            strGetFields.Append(" a.ReturnVisitID main,b.MemberCardID,e.Name,c.ConsumeItem, ");
            strGetFields.Append(" case when a.ReturnVisitType ='1' then '电话' end as ReturnVisitType, ");
            strGetFields.Append(" a.StartTime,case when a.EndTime='1900/1/1 0:00:00' then '' else a.EndTime end as EndTime, ");
            strGetFields.Append(" a.Satisfaction as SatisfactionID,a.State as StateID, ");
            strGetFields.Append(" case when a.Satisfaction = '1' then '不满意' else '满意' end as Satisfaction, ");
            strGetFields.Append(" case when a.State ='1' then '登记状态' when a.State ='2' then '执行状态' else '完成状态' end as State, ");
            strGetFields.Append(" d.MemberID,b.ShopLeader,e.VehicleNo,b.ConsumeTime,a.SendEmployee,  a.Complaint, a.Result, a.Memo ");

            StringBuilder strWhere = new StringBuilder(); //查询的条件
            strWhere.AppendFormat(" a.ReturnVisitID=N\'{0}\' ", PK);

            object[] obj = new object[] { tblName.ToString(), strGetFields.ToString(), strOrder, 1, 1, strWhere };
            DataTable dt = new DataTable();
            dt = dalSqlHelper.ExecuteProcedure(obj, ref RsCount);
            if (dt.Rows.Count > 0)
            {
                return dalSqlHelper.DtToSON(dt);
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// 获取会员信息
        /// </summary>
        /// <returns></returns>
        public static string GetEp()
        {
            sqlobj = new SqlObject();
            sqlobj.Sql = "select EmployeeID,Name from T_CF_OA_Employee where 1=@one ";
            sqlobj.PramS = new SqlParameter[] { 
               new SqlParameter("@one",1)
            };
            dt = dalSqlHelper.ExecuteSelect(sqlobj);
            return dalSqlHelper.DtToSON(dt);
        }
        #endregion


    }
}
